package com.usian.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.usian.common.enums.AppHttpCodeEnum;
import com.usian.common.exception.LeadException;
import com.usian.common.util.BeanHelper;
import com.usian.common.util.JwtUtils;
import com.usian.model.media.dtos.WmUserDto;
import com.usian.model.media.dtos.WmUserLoginDto;
import com.usian.wemedia.entity.WmUser;
import com.usian.wemedia.mapper.WmUserMapper;
import com.usian.wemedia.service.IWmUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 自媒体用户信息表 服务实现类
 * </p>
 *
 * @author usian
 * @since 2022-09-30
 */
@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements IWmUserService {

    @Override
    public WmUserDto findByApUserId(Integer apUserId) {
        QueryWrapper<WmUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(WmUser::getApUserId,apUserId);
        WmUser wmUser = this.getOne(queryWrapper);
        if(wmUser!=null){
            WmUserDto wmUserDto = BeanHelper.copyProperties(wmUser, WmUserDto.class);
            return wmUserDto;
        }
        return null;
    }

    @Override
    public Integer saveWmUser(WmUserDto wmUserDto) {
        //判断自媒体用户是否已经存在
        WmUserDto userDto = this.findByApUserId(wmUserDto.getApUserId());
        if(userDto!=null){
            //自媒体用户已经存在
            throw new LeadException(AppHttpCodeEnum.DATA_EXIST,"自媒体用户已存在");
        }

        //转为WmUser对象
        WmUser wmUser = BeanHelper.copyProperties(wmUserDto, WmUser.class);
        wmUser.setCreatedTime(new Date());
        //保存自媒体用户
        boolean b = this.save(wmUser);
        if(!b){
            throw new LeadException(AppHttpCodeEnum.SERVER_ERROR);
        }

        return wmUser.getId();
    }

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Override
    public Map<String, Object> login(WmUserLoginDto dto) {
        //1、参数校验
        if(dto==null){
            throw new LeadException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2、根据用户名查询用户是否存在
        QueryWrapper<WmUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(WmUser::getName,dto.getName());
        WmUser wmUser = this.getOne(queryWrapper);
        if(wmUser==null){
            throw new LeadException(AppHttpCodeEnum.LOGIN_USERNAME_ERROR);
        }
        //3、校验密码是否正确
        boolean matches = passwordEncoder.matches(dto.getPassword(), wmUser.getPassword());
        if(!matches){
            throw new LeadException(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }

        //4、生成token
        String token = JwtUtils.generateTokenExpireInMinutes(wmUser.getId(), 60);

        //5、组装返回的结果
        Map<String,Object> map = new HashMap<>();
        wmUser.setPassword("");
        wmUser.setEmail("");
        map.put("token",token);
        map.put("user",wmUser);
        return map;
    }
}
